package com.tianlang.security;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.tianlang.util.StringUtil;

/**
 * 
 * @author mchp SpringSecurity兑现多登录成功页面
 *
 */
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

	@SuppressWarnings("unused")
	private static Logger LOG = Logger.getLogger(MyAuthenticationSuccessHandler.class);

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth)
			throws IOException, ServletException {
		String f = request.getParameter("f");
		if (StringUtil.isNotEmpty(f)) {
			if (f.equals("android")) {
				response.setCharacterEncoding("UTF-8");
				response.getWriter().write("登录成功");
			}
		} else {
			String ru = (String) request.getSession().getAttribute("ru");
			request.getSession().removeAttribute("ru");
			if (StringUtil.isNotEmpty(ru)) {
				response.sendRedirect(ru);
				// request.getRequestDispatcher(ru).forward(request, response);
			} else {
				request.getRequestDispatcher("/account/user").forward(request, response);
			}
		}

	}

}
